<?php 

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header("Content-type:application/pdf");
if ($_POST['table'] == "") {
	echo "Post parameter <table_name> <record_id>(optional)";
	return;
}

include '../DB/dbConnection.php';

if ($_POST['id'] != "-1") {
	print_r(getRecord($_POST['id']));
} else {
	print_r(getAllRecord());
}

function getRecord($id){
	
	global $conn;
	
	$sql = "SELECT * FROM ".$_POST['table']." WHERE id=".$id;
	
	$res = mysqli_query($conn, $sql);
	
	if ($row = mysqli_fetch_assoc($res)) {
		return json_encode($row);
	}
}

function getAllRecord(){
	
	global $conn;
	
	$sql = "SELECT * FROM ".$_POST['table'];
	
	$res = mysqli_query($conn, $sql);

	$a=array();

	while ($row = mysqli_fetch_assoc($res)) {
		array_push($a,$row);
	}
	return "{\"arrayList\":".json_encode($a)."}";
}

$conn->close();
?>